package org.zerta.troops.common;
import java.util.Map;
import java.util.HashMap;

public class Army {
	
	private Map <String, Integer> armyUnits;
	private Player owner;
	
	public Army ( ) {
		armyUnits = new HashMap<String, Integer>();
	}
	
	public Player getOwner() {
		return owner;
	}
	
	public void setOwner( Player owner ) {
		this.owner = owner;
	}
	
	public int changeArmyUnit( Unit unit, int num ) {
		if ( num == 0 ) {
			return 0;
		}
		String unitName = unit.getName().toLowerCase();
		if (armyUnits.containsKey(unitName)) {
			Integer unitNum = armyUnits.remove(unitName);
			int newNum = unitNum.intValue() + num;
			if (num < 0) {
				if (newNum < 0) {
					// Return that all units have been removed
					return unitNum.intValue();
				} else {
					// Return the number of removed units
					return -num;
				}
			} else {
				Integer unitNumNew = new Integer (newNum);
				armyUnits.put( unitName, unitNumNew );
				// Return the number of added units
				return newNum;
			}
		} else {
			if (num < 0) {
				// Return that no units have been removed
				return 0;
			} else {
				Integer unitNum = new Integer ( num );
				armyUnits.put( unitName, unitNum );
				// Return the number of added units
				return num;
			}
		}
	}
	
	public int getArmyUnit ( Unit unit ) {
		if (armyUnits.containsKey(unit.getName())) {
			return armyUnits.get(unit.getName());
		} else {
			return 0;
		}
	}
	
	public int addArmyUnit( Unit unit, int num) {
		return changeArmyUnit( unit, num );
	}
	
	public int removeArmyUnit( Unit unit, int num ) {
		return changeArmyUnit( unit, -num );
	}
	

}
